package com.wugui.datax.admin.tool.query;

import com.wugui.datax.admin.core.conf.JobAdminConfig;
import com.wugui.datax.admin.entity.JobDatasource;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;

import java.io.IOException;
import java.sql.SQLException;

/**
 * hive
 *
 * @author wenkaijing
 * @version 2.0
 * @since 2020/01/05
 */
@Slf4j
public class HiveQueryTool extends BaseQueryTool implements QueryToolInterface {
    public HiveQueryTool(JobDatasource jobDatasource) throws SQLException {

        log.debug("UseKerberos={}",JobAdminConfig.getAdminConfig().getUseKerberos());
        if(JobAdminConfig.getAdminConfig().getUseKerberos()){
            System.setProperty("sun.security.krb5.debug", "true");
            System.setProperty("java.security.krb5.conf", JobAdminConfig.getAdminConfig().getKrb5Conf());
            System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");

            Configuration conf = new Configuration();
            conf.set("hadoop.security.authentication", "Kerberos");

            try {
                UserGroupInformation.loginUserFromKeytab(JobAdminConfig.getAdminConfig().getHiveKeyTabUser(), JobAdminConfig.getAdminConfig().getHiveKeyTab());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        init(jobDatasource);

    }
}
